package handler

import (
	"big-fleet-mgr/chone"
	"big-fleet-mgr/modler"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	var count int64
	modler.DB.Model(&modler.CarOrder{}).Count(&count)
	/*
		SELECT
		  car_orders.id AS OrderId,
		  car_orders.car_order_code AS CarOrderCode,
		  car_orders.car_order_name AS CarOrderName,
		  car_orders.car_order_price AS CarOrderPrice,
		  car_types.car_name AS CarName
		FROM
		  car_orders
		  LEFT JOIN car_types ON car_types.id = car_orders.tid
	*/

	var list []modler.CarOrderList
	redis_key := "CarList" + strconv.Itoa(page)
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Table("car_orders").
			Select(

				"car_orders.id AS OrderId",
				"car_orders.car_order_code AS CarOrderCode",
				"car_orders.car_order_name AS CarOrderName",
				"car_orders.car_order_price AS CarOrderPrice",
				"car_types.car_name AS CarName",
			).
			Joins("LEFT JOIN car_types ON car_types.id = car_orders.tid").
			Offset(offset).
			Limit(size).
			Debug().
			Find(&list)
		marshal, _ := json.Marshal(list)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"count": count,
	})
	return

}

func CarDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  car_orders.id AS OrderId,
		  car_orders.car_order_code AS CarOrderCode,
		  car_orders.car_order_name AS CarOrderName,
		  car_orders.car_order_price AS CarOrderPrice,
		  car_types.car_name AS CarName
		FROM
		  car_orders
		  LEFT JOIN car_types ON car_types.id = car_orders.tid
		WHERE
		  car_orders.id = 2

	*/
	var list modler.CarOrderList
	redis_key := "CarDetail" + id
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Table("car_orders").
			Select(

				"car_orders.id AS OrderId",
				"car_orders.car_order_code AS CarOrderCode",
				"car_orders.car_order_name AS CarOrderName",
				"car_orders.car_order_price AS CarOrderPrice",
				"car_types.car_name AS CarName",
			).
			Joins("LEFT JOIN car_types ON car_types.id = car_orders.tid").
			Where("car_orders.id", id).
			Debug().
			Find(&list)
		marshal, _ := json.Marshal(list)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return

}

func UserCar(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN roles ON roles.id = user_roles.rid
	*/
	var list modler.Role
	modler.DB.Table("roles").
		Joins(" LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid", uid).
		First(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func NodeRole(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
	*/
	var list modler.Node
	modler.DB.Table("nodes").
		Joins(" LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid=?", rid).
		First(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
